﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XMLGener.Interfaces;
using XMLGener.Data;
namespace XBasicModules
{
    /// <summary>
    /// 
    /// </summary>
    public class Enumerator : ModuleBase, XMLGenerModuleInterface
    {
        #region XMLGenerModuleInterface Members

        private string order = "random";
        private bool randomListing = false;
        private Random rnd = new Random();
        private List<string> list = new List<string>();

        private int index = -1;
        /// <summary>
        /// Return user defined string.
        /// </summary>
        /// <returns></returns>
        public string generate()
        {
            if (randomListing)
            {                
                index = rnd.Next(0,list.Count);
            }
            else
                index++;
            if (index >= list.Count)
                index = 0;
            if (list.Count==0)
                return "";
            return list[index];
        }

        /// <summary>
        /// return this module definition
        /// </summary>
        /// <returns>definition</returns>
        public virtual string getDefinition()
        {
            return
              "<function>" +
              "    <name>Enumerator</name>" +
              "    <id>enumenator</id>" +
              "    <type>string</type>" +
              "    <attributes>" +
              "       <attribute>" +
              "          <name>Order</name>" +
              "          <id>order</id>" +
              "          <type>select</type>" +
              "          <items>" +
              "             <item name=\"Random order\" value=\"random\"/>" +
              "             <item name=\"Sequence\" value=\"sequence\"/>" +
              "          </items>" +
              "       </attribute>" +
              "    </attributes>" +
              "   <acceptrestriction>enumeration</acceptrestriction>" +

              "</function>";

        }

        /// <summary>
        /// set attributes to this module
        /// </summary>
        /// <param name="xmlParams"></param>
        /// <param name="parentDocument"></param>
        public void setParameters(string xmlParams, XMLGenerDocument parentDocument)
        {
            if (!this.checkSettingsElement(xmlParams, parentDocument))
                return;
            this.order = this.getParam("order");
            if (order == "random")
            {
                randomListing = true;
            }
            if (restrictions != null && restrictions.useEnumeration)
            {
                this.list = restrictions.getEnumerationInList();
            }

        }

        #endregion

    }
}
